/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PesquisaAluno.java
 *
 * Created on 25/06/2011, 14:41:47
 */

package pesquisar;

import utilitarios.ConnectionHelper;
import visao.Telaprincipal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Jean
 */
public class PesquisaAluno extends javax.swing.JFrame {
    
    private Connection conn = null;
    private PreparedStatement pstm = null;
    private ResultSet rs = null;

    /** Creates new form PesquisaAluno */
    public PesquisaAluno(Telaprincipal t) {
        try {
            initComponents();
            this.setLocationRelativeTo(t); 
            conn = ConnectionHelper.getIntance().getConnection();
            
            pstm = conn.prepareStatement("select al.AL_NOME, tu.TU_DESCRICAO, cu.CU_DESCRICAO "
                            +"from app.matricula ma "
                            +"inner join app.aluno al on al.AL_ID = ma.MA_IDALUNO "
                            +"inner join app.turma tu on tu.TU_ID = ma.MA_IDTURMA "
                            +"inner join app.curso cu on cu.CU_ID = ma.MA_IDCURSO ");
            pstm.execute();        
            rs = pstm.getGeneratedKeys();
            
            preencher_Jtable();
        } catch (SQLException ex) {
            Logger.getLogger(PesquisaAluno.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Aluno", "Turma", "Curso"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setText("Listando todos alunos matriculados:");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(110, 110, 110)
                        .add(jLabel1))
                    .add(layout.createSequentialGroup()
                        .add(23, 23, 23)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 350, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(28, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(10, 10, 10)
                .add(jLabel1)
                .add(18, 18, 18)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 256, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(56, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               // new PesquisaAluno().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    public void preencher_Jtable()
    {
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(150);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(50);
        jTable1.getColumnModel().getColumn(2).setPreferredWidth(200);
        
        DefaultTableModel modelo = (DefaultTableModel)jTable1.getModel();
        modelo.setNumRows(0);
        try
        {
            while(rs.next())
                modelo.addRow(new Object[]{rs.getString("AL_NOME"),rs.getString("TU_DESCRICAO"),rs.getString("CU_DESCRICAO")});
        }
        catch(SQLException eroo)
        {
            JOptionPane.showMessageDialog(null,"erro ao inserir dados na paradinha "+ eroo);
        }
    }

}
